package file.day9_20.day05;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    private Socket socket;
    public Client(){
            try {
                socket = new Socket("localhost",8088);//寻找端口号为8088的服务器进行连接
                System.out.println("与服务器建立连接");
            } catch (IOException e) {
                e.printStackTrace();
            }

    }

    public void start(){
        try(
                PrintWriter pw = new PrintWriter(
                        new BufferedWriter(
                                new OutputStreamWriter(
                                        socket.getOutputStream(),"UTF-8"//指定写出socket信息块
                                )
                        ),true
                );
        ){

            //完成写出socket信息块，发送信息
            Scanner scanner = new Scanner(System.in);
            while (true){
                System.out.println("请输入要发送的消息：");
                String message = scanner.nextLine();
                if(message.equals("exit"))break;
                pw.println(message);
            }

        }catch (IOException e){
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
